package com.xp.framework;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:xuecx
 * @descript:<p> </p>
 * @date:2019/4/29
 */
public class RpcInvokerHandler {
    public static Map<String, Object> serviceMap = new HashMap<String, Object>();

    public static RpcResponse invokeService(RpcRequest request) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Object serviceBean = serviceMap.get(request.getClassName());

        RpcResponse response = new RpcResponse();
        response.setRequestId(request.getRequestId());
        try {
            if (serviceBean != null) {
                Class<?> serviceClass = serviceBean.getClass();
                String methodName = request.getMethodName();
                Class<?>[] parameterTypes = request.getParameterTypes();
                Object[] parameters = request.getParameters();

                Method method = serviceClass.getMethod(methodName, parameterTypes);
                method.setAccessible(true);
                Object result = method.invoke(serviceBean, parameters);
                response.setResult(result);
            }
        } catch (Throwable t) {
            t.printStackTrace();
            response.setError(t);
        }
        return response;
    }
}
